<?php


namespace app\index\controller;


use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use app\index\model\Image as ImageModel;
use think\facade\Log;

class Image
{
    public function imageToThumb($name,$type){

        $name1= "../public/resource/".$name.".".$type;
        $image = \think\Image::open($name1);
        $name2 = "../public/resource/thumb/".$name.".".$type;
        $image->thumb(1000, 1000)->save($name2);
        return $name.".".$type.":成功转换";
    }

    public function selectFirstImage(){
            $data = new ImageModel();
            try {
                $data = $data->where("image_delete_time",null)
                    ->order("image_like desc")->limit(10)->select();
            } catch (DataNotFoundException $e) {
            } catch (ModelNotFoundException $e) {
            } catch (DbException $e) {
                Log::warning("查询数据库错误");
            }
            return $data->toArray();
    }

//    public function imageToThumb($image){
//        $image = \think\Image::open("/static/".$image);
//        return $image->thumb(150, 150);
//    }
    public function countImage(){
        $data = new ImageModel();
        try {
            $data = $data->selectOrFail();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
        $array = $data->toArray();
        return sizeof($array);
    }
}